package com.monkey.web.context.strategy;

import com.monkey.core.web.response.MonkeyResponseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/9/1
 */
public class CtrlReturnValueResolve {
    public static Logger logger = LoggerFactory.getLogger(CtrlReturnValueResolve.class);
    private List<CtrlReturnValueHandler> handlers = new ArrayList<>();

    public CtrlReturnValueResolve() {
    }


    public CtrlReturnValueResolve addHandler(CtrlReturnValueHandler handler) {
        this.handlers.add(handler);
        return this;
    }

    public MonkeyResponseDto resolve(Object returnValue, String fieldStr) {
        for (CtrlReturnValueHandler handler : handlers) {
            if (handler.support(returnValue)) {
                return handler.handle(returnValue, fieldStr);
            }
        }
//        throw new IllegalArgumentException("不能找到该Controller返回值的解析处理器");
        logger.warn("不能找到该Controller返回值的解析处理器,原样返回");
        return null;
    }


}
